import cv2
import matplotlib.pyplot as plt

# 定义一个类（双线性插值计算）
class bilinear_interpolation:
    P1 = P2 = P3 = P4 = 0

    # 计算图中绿色三角像素值
    def green(self):
        Q12_green = 0.3 * self.P2 + 0.7 * self.P1
        Q34_green = 0.3 * self.P4 + 0.7 * self.P3
        PQ_green = 0.3 * Q34_green + 0.7 * Q12_green
        return PQ_green

    # 计算图中蓝色三角像素值
    def blue(self):
        Q12_blue = 0.6 * self.P2 + 0.4 * self.P1
        Q34_blue = 0.6 * self.P4 + 0.4 * self.P3
        PQ_blue = 0.3 * Q34_blue + 0.7 * Q12_blue
        return PQ_blue

    # 计算图中紫色三角像素值
    def purple(self):
        Q12_purple = 0.3 * self.P2 + 0.7 * self.P1
        Q34_purple = 0.3 * self.P4 + 0.7 * self.P3
        PQ_purple = 0.6 * Q34_purple + 0.4 * Q12_purple
        return PQ_purple

    # 计算图中黄色三角像素值
    def yellow(self):
        Q12_yellow = 0.6 * self.P2 + 0.4 * self.P1
        Q34_yellow = 0.6 * self.P4 + 0.4 * self.P3
        PQ_yellow= 0.6 * Q34_yellow + 0.4 * Q12_yellow
        return PQ_yellow


BI = bilinear_interpolation()

# 读取图像
img1 = cv2.imread("sunset.jpg")
img2 = cv2.imread("img4.png") # 制作一个模板

# 索引列表（位置坐标）
list1 = list(range(0, 322, 2))
list2 = list(range(0, 644, 4))
list3 = list(range(0, 500, 2))
list4 = list(range(0, 1000, 4))

# 放大图像（双线性插值）
for i,u in zip(list1, list2):
    for j,v in zip(list3, list4):
        img2[u, v], img2[u, v+3], img2[u+3, v], img2[u+3, v+3] = img1[i, j], img1[i, j+1], img1[i+1, j], img1[i+1, j+1] # 原图像未更改像素值
        BI.P1 = img1[i, j]; BI.P2 = img1[i, j + 1]; BI.P3 = img1[i + 1, j]; BI.P4 = img1[i + 1, j + 1]

        img2[u, v+1] = img2[u+1, v] = img2[u+1, v+1] =BI.green()
        img2[u, v+2] = img2[u+1, v+3] = img2[u+1, v+2] =BI.blue()
        img2[u+2, v] = img2[u+3, v+1] = img2[u+2, v+1] =BI.purple()
        img2[u+3, v+2] = img2[u+2, v+3] = img2[u+2, v+2] =BI.yellow()

# 保存图像
cv2.imwrite('enlarged.jpeg',img2)
